<template>
  <div class="business-all-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <!-- <el-button :icon="Plus" type="primary" @click="handleEdit">
          添加一个新客户
        </el-button> -->
        <el-button :icon="Delete" type="primary" @click="handleDelete">
          批量调配
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.TrackStaff"
              clearable
              placeholder="按跟进人"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.searchKey"
              clearable
              placeholder="按客户名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane
        v-for="(item, index) in tabList"
        :key="index"
        :label="item.label"
        :name="item.value"
      >
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="客户ID"
            width="100"
            prop="CustomerID"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="商机ID"
            width="100"
            prop="OrderID"
            show-overflow-tooltip
          />

          <el-table-column
            align="center"
            label="创建商机时间"
            prop="addTime"
            show-overflow-tooltip
          />

          <el-table-column
            v-if="queryForm.tab == 'signed'"
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="商机名称"
            prop="BusinessOrderName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="客户名称"
            prop="CustomerAllName"
            show-overflow-tooltip
          />

          <el-table-column
            v-if="queryForm.tab == 'signed'"
            align="center"
            label="订单总金额"
            prop="OrderTotalMoney"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="queryForm.tab == 'signed'"
            align="center"
            label="产品总金额"
            prop="ProductTotalMoney"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="queryForm.tab != 'recommend'"
            align="center"
            label="跟进人"
            prop="SaleStaffName"
            show-overflow-tooltip
          />

          <el-table-column align="center" label="操作" width="400">
            <template #default="{ row }">
              <el-button type="primary" @click="showBusinessPath(row.OrderID)">
                商机流转过程
              </el-button>
              <el-button
                v-if="queryForm.tab != 'recommend'"
                type="primary"
                @click="getCustomerTrack(row)"
              >
                查看跟进记录
              </el-button>
              <el-button
                v-if="queryForm.tab != 'signed'"
                type="primary"
                @click="handleEdit(row, 0)"
              >
                商机调配
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
    <business-path ref="BusinessPathRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { allBusinessList } from '@/api/businessAll'
  import Edit from './components/BusinessAllEdit'
  import BusinessPath from '@/views/components/BusinessPath'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'AllBusiness',
    components: { Edit, BusinessPath },
    setup() {
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        tabList: [
          {
            label: '跟进中',
            value: 'tracking',
          },
          {
            label: '已签约',
            value: 'signed',
          },
          {
            label: '已放弃',
            value: 'giveup',
          },
          // {
          //   label: '推荐未挑入',
          //   value: 'recommend',
          // },
        ],
        BusinessPathRef: null,
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          tab: 'tracking',
          pageNo: 1,
          pageSize: 10,
          searchKey: '',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row, moreid) => {
        if (moreid) {
          state.editRef.showEdit(row, state.queryForm, moreid)
        } else {
          state.editRef.showEdit(row, state.queryForm, moreid)
        }
      }
      const handleDelete = (row) => {
        if (row.OrderID) {
          $baseConfirm('你确定要调配当前项吗', null, async () => {
            handleEdit(row, 0)
            // alert('尚未开放批量调配，请稍侯！')
            // const { msg } = await doDelete({ ids: row.OrderID })
            // $baseMessage(msg, 'success', 'vab-hey-message-success')
            // await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.OrderID).join()
            $baseConfirm('慎用！你确定要批量调配选中项吗', null, async () => {
              console.log(ids)
              handleEdit(ids, 1)
              // const { msg } = await doDelete({ ids })
              // $baseMessage(msg, 'success', 'vab-hey-message-success')
              // await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }

      const handleTabClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await allBusinessList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      const getCustomerTrack = (row) => {
        router.push({
          path: '/customer/trackingList',
          query: { CustomerPickID: row.CustomerPickID, OrderID: row.OrderID },
        })
      }
      const showBusinessPath = (businessPath) => {
        state.BusinessPathRef.showEdit(businessPath)
      }
      return {
        ...toRefs(state),
        setSelectRows,
        handleEdit,
        handleDelete,
        handleSizeChange,
        getCustomerTrack,
        handleCurrentChange,
        handleTabClick,
        queryData,
        fetchData,
        showBusinessPath,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
